﻿function onMouseMoveTheRoom(element)
{

}


function onMouseOutTheRoom(element)
{
	
}

$(document).ready(function(){
	//获取当前登录学生的选房信息			
	$.post("AjaxAction!getChoosenInfo.action", null,
			function(data) {
				if (data[0].result == "logout") {
					$('#chooseState').html('未登录！');
					$('#chooseRoom').html('无');
					$('#chooseBed').html('无');
				} else if (data[0].result == "error") {
					alert("获取选房信息发生错误！请刷新页面重试，如问题依然存在，请与管理员联系");
				} else if (data[0].result == "false") {
					$('#chooseState').html('未选房！');
					$('#chooseRoom').html('无');
					$('#chooseBed').html('无');
				} else if (data[0].result == "true") {
					$('#chooseState').html('已选');
					$('#chooseRoom').html(data[0].room_number);
					$('#chooseBed').html(data[0].bed_id);
				} else {
					alert("错误，可能是网络不通，请稍后重试，如果问题一直存在，请与管理员联系！");
				}
			}, "json");
});

function setRoomState(room, isOpen, event, color) {
	var newid = room.attr('id');
	if (color == null) {
		color = "black";
		newid = "new_" + newid;
	}
	if (isOpen) {
		$('#new_' + room.attr('id')).remove();
	} else {
		var offset = room.offset();
		$("<div/>").css({
			"position" : "absolute",
			"left" : offset.left,
			"top" : offset.top,
			"width" : room.width(),
			"height" : room.height(),
			"backgroundColor" : color,
			"opacity" : "0.5"
		}).attr('id', newid).click(event).appendTo('#__01');
	}
}
function showRoom() {
	var room = this.id;
	var url = parent.window.location.href;
	var floor = $.query.load(url).get("f");
	$.post("AjaxAction!getBedInfo.action", {
		room_number : room,
		floor : floor
	}, function(data) {
		$('#bed').empty();
		if (data[0]) {
			showBed(data);
		}
	}, "json");
}
function handleselbed(ret)
{
    if(ret == 0)
    {
    	window.alert("您已经选过房间了");
    }
    else if(ret == 1)
    	window.alert("房间已选好");
    else if(ret == 3)
    	window.alert("床位已满");
}
function bedFunction(id,isSelected,gender)
{
	if (gender == 0)
		gender = "男";
	else
		gender = "女";
	if(isSelected == 1)/*已经被选择了*/
	{
		window.alert("该床位已经被选了，请选择其他房间");
	}
	else if(isSelected == 0)
	{
		if($('#studentID').attr("value")=="nobody")
		{
			if(confirm("您好，您还没有登录,登录以后才可选房，是否立即登录？")){
				top.location.href="login.html";
			}
			return false;
		}
		if(!window.confirm("该房间为"+gender+"生房间，您确认要选择这个床位吗？"))
			return false;	
		var room = id.substring(0, id.length - 3);
		var bed = id.substring(id.length - 1);
		$.post("AjaxAction!getChoosenInfo.action",null,function(data){
			if(data[0].result=="logout"){
				if(confirm("您好，您还没有登录,登录以后才可选房，是否立即登录？")){
					parent.window.location.href="login.html";
				}
				$('#chooseState').html('未登录！');
				$('#chooseRoom').html('无');
				$('#chooseBed').html('无');
				return false;
			}else if(data[0].result=="error"){
				alert("错误！请与管理员联系");
			}else if(data[0].result=="false"){
				realChooseThisBed(room,bed);
			}else if(data[0].result=="true"){
				if(confirm("您已经选择了"+data[0].room_number
						+"房间"+data[0].bed_id
						+"床位，是否继续选择？(之前的选房将取消)")){
					realChooseThisBed(room,bed);
				}else{
					return false;
				}
			}else{
				alert("错误，可能是网络不通，请稍后重试，如果问题一直存在，请与管理员联系！");
			}
		},"json");
		//realChooseThisBed(bed);
		//bed.style.backgroundImage = "url(./images/bedselect.png)";
		//bed.setAttribute("onclick","bedFunction('"+id+"',1)");
		
	}
}
function realChooseThisBed(room,bed)
{
	$.post("AjaxAction!selectBed.action",{
		room_number : room,
		bed_id : bed
	},function(data){
		if(data[0].result=="logout"){
			if(confirm("您好，您还没有登录,登录以后才可选房，是否立即登录？")){
				parent.window.location.href="login.html";
			}
		}else if(data[0].result=="bedChoosen"){
			alert("抱歉，该床位已经被其他人选择，请选择其他床位或者更换房间！");
			showRoom(room);
		}else if(data[0].result=="roomFull"){
			alert("抱歉，该房间已满，请更换房间！");
		}else if(data[0].result=="error"){
			alert("抱歉，查询床位信息出错，请稍后重试，如果问题依然存在，请与管理员联系！");
		}else if(data[0].result=="timeerror"){
			alert("错误！不在合法选房时间内");
		}else if(data[0].result=="success"){
			alert("恭喜，选房成功！您已经选择了"+data[0].room_number+"房间"+data[0].bed_id+"床位");
			$('#chooseState').html('已选');
			$('#chooseRoom').html(data[0].room_number);
			$('#chooseBed').html(data[0].bed_id);
			$('#'+room).click();		//更新床位数据展示
			//showRoom(room);
		}else if(data[0].result=="genderUnmatched"){
			alert("抱歉，您的性别与该房间的可选性别不符合！");
		}
	},"json");
}
/*
 * what is going to be done is just how to use the information successfully
 * when we choose the room we have to see the current status of the room,and we must make it useful 
 * that we make the room 
 */
function showBed(data)
{
	var type=data[0].type;
	var thisroom = document.getElementById("bed");
	var gender = data[0].gender_type;
	if(type == 4)
	{
		var isChoosen = data[0].isChoosen;
		var bed1 = document.createElement("DIV");
		bed1.id = data[0].room_number+"_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '1px';
		bed1.style.top = '62px';
		if(isChoosen == 1)
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick","bedFunction('"+bed1.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		isChoosen = data[1].isChoosen;
		bed2.id = data[0].room_number+"_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "61px";
		bed2.style.top = "62px";
		if(isChoosen == 1)
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick","bedFunction('"+bed2.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed2);
		var bed3 = document.createElement("DIV");
		isChoosen = data[2].isChoosen;
		bed3.id = data[0].room_number+"_b3";
		bed3.style.position = "absolute";
		bed3.style.left = "128px";
		bed3.style.top = "62px";
		if(isChoosen == 1)
			bed3.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed3.style.backgroundImage = 'url(./images/bednormal.png)';
		bed3.style.width = "58px";
		bed3.style.height = "50px";
		bed3.setAttribute("onclick","bedFunction('"+bed3.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed3);
		var bed4 = document.createElement("DIV");
		isChoosen = data[3].isChoosen;
		bed4.id = data[0].room_number+"_b4";
		bed4.style.position = "absolute";
		bed4.style.left = "189px";
		bed4.style.top = "62px";
		if(isChoosen == 1)
			bed4.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed4.style.backgroundImage = 'url(./images/bednormal.png)';
		bed4.style.width = "58px";
		bed4.style.height = "50px";
		bed4.setAttribute("onclick","bedFunction('"+bed4.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed4);
	}
	else if(type == 2)
	{
		var bed1 = document.createElement("DIV");
		var isChoosen = data[0].isChoosen;
		bed1.id = data[0].room_number+"_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '47px';
		bed1.style.top = '60px';
		if(isChoosen ==1 )
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick","bedFunction('"+bed1.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		isChoosen = data[1].isChoosen;
		bed2.id = data[0].room_number+"_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "145px";
		bed2.style.top = "60px";
		if(isChoosen ==1 )
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick","bedFunction('"+bed2.id+"',"+isChoosen+","+gender+")");
		thisroom.appendChild(bed2);
	}
	else if(type == 3)
	{
		var bed1 = document.createElement("DIV");
		bed1.id = data[0].room_number+"_b1";
		bed1.style.position = "absolute";
		bed1.style.left = '5px';
		bed1.style.top = '59px';
		if(data[0].isChoosen == 1)
			bed1.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(data[0].isChoosen == 0)
			bed1.style.backgroundImage = 'url(./images/bednormal.png)';
		bed1.style.width = "58px";
		bed1.style.height = "50px";
		bed1.setAttribute("onclick","bedFunction('"+bed1.id+"',"+data[0].isChoosen+","+gender+")");
		thisroom.appendChild(bed1);
		var bed2 = document.createElement("DIV");
		bed2.id = data[0].room_number+"_b2";
		bed2.style.position = "absolute";
		bed2.style.left = "180px";
		bed2.style.top = "59px";
		if(data[1].isChoosen == 1)
			bed2.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(data[1].isChoosen == 0)
			bed2.style.backgroundImage = 'url(./images/bednormal.png)';
		bed2.style.width = "58px";
		bed2.style.height = "50px";
		bed2.setAttribute("onclick","bedFunction('"+bed2.id+"',"+data[1].isChoosen+","+gender+")");
		thisroom.appendChild(bed2);
		var bed3 = document.createElement("DIV");
		bed3.id = data[0].room_number+"_b3";
		bed3.style.position = "absolute";
		bed3.style.left = "5px";
		bed3.style.top = "155px";
		if(data[2].isChoosen == 1)
			bed3.style.backgroundImage = 'url(./images/bedselect.png)';
		else if(data[2].isChoosen == 0)
			bed3.style.backgroundImage = 'url(./images/bednormal.png)';
		bed3.style.width = "58px";
		bed3.style.height = "50px";
		bed3.setAttribute("onclick", "bedFunction('"+bed3.id+"',"+data[2].isChoosen+","+gender+")");
		thisroom.appendChild(bed3);
	}else if(type==1){
		var id=data[0].room_number+"_b1";
		var db1=$("<div/>").css({
			"left":'5px',
			"top":'59px',
			"width":'58px',
			"height":'50px',
			"position":'absolute'
		}).attr("id", id).click(function(){
			bedFunction(id, data[0].isChoosen, gender);
		}).appendTo('#bed');
		if(data[0].isChoosen==1){
			db1.css('background-image', 'url(./images/bedselect.png)');
		}else if(data[0].isChoosen == 0){
			db1.css('background-image', 'url(./images/bednormal.png)');
		}
	}
	/*if(type == 1)
		bed.style.backgroundImage="url(./images/layout1.png)";
	else if(type == 2)
		bed.style.backgroundImage="url(./images/layout2.png)";
	else if(type == 3)
		bed.style.backgroundImage="url(./images/layout3.png)";*/
	thisroom.style.backgroundImage = 'url(./images/layout3back.jpg)';
	thisroom.style.visibility = "visible";
}
function full()
{
	window.alert("您好，这个房间已经选满了，请选择其他房间!");
}
function closed()
{
	window.alert("抱歉，该房间已经被关闭，您无法选择!");
}
function showThisRoom(element)
{
	alert('aaa');
	var idString = element.id;
	var dormNum = idString.substring(1,3);
	var layNum = idString.substring(4,6);
	var roomNum = idString.substring(8,10);
	var oldDiv = document.getElementById(idString);
	var top = oldDiv.offsetTop;
	var left= oldDiv.offsetLeft;
	var width= oldDiv.offsetWidth;
	var height = oldDiv.offsetHeight;
	var newDiv = document.createElement("DIV");
	newDiv.id="changeDiv";
	newDiv.style.backgroundImage='url(./images/divBack.png)';
	newDiv.style.backgroundRepeat='repeat';
	newDiv.style.position='absolute';	
	newDiv.style.left = left;
	newDiv.style.top = top;
	newDiv.style.width = width;
	newDiv.style.height = height;
	newDiv.setAttribute("onclick","window.alert(djijf)");
	var mainDiv=document.getElementById("__01");	
	mainDiv.appendChild(newDiv);
}
